﻿// <copyright file="TTablePropertyInfo.cs" company="N/A">
//     Copyright (c) Nicholas Barrett and Benjamin Kasten.  All rights reserved.
//
//     This program is free software; you can redistribute it and/or modify it
//     under the terms of the GNU General Public License as published by the
//     Free Software Foundation; either version 2 of the License, or (at your
//     option) any later version.
//
//     This program is distributed in the hope that it will be useful, but
//     WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
//     Public License for more details.
//
//     You should have received a copy of the GNU General Public License along
//     with this program; if not, write to the Free Software Foundation, 
//     Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// </copyright>

namespace BetterAzureTableStorage
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;

    /// <summary>
    /// This class is used to contain the PropertyInfo and field name for a given property
    /// </summary>
    /// <remark>
    /// This class is intended to replace the use of Tuple{PropertyInfo, string} when storing Properties in table storage
    /// </remark>
    public class TTablePropertyInfo
    {
        /// <summary>
        /// Gets or sets the info.
        /// </summary>
        /// <value>The property info.</value>
        public PropertyInfo Info { get; set; }

        /// <summary>
        /// Gets or sets the alias names of the propertyinfo associated with this instance.
        /// </summary>
        public List<string> Aliases { get; set; }

        /// <summary>
        /// Gets or sets the name of the field.
        /// </summary>
        /// <value>The name of the field.</value>
        public string FieldName { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="TTablePropertyInfo"/> class.
        /// </summary>
        /// <param name="propInfo">The property info object.</param>
        /// <param name="fieldName">Name of the field.</param>
        /// <param name="aliases">The aliases of the field.</param>
        public TTablePropertyInfo(PropertyInfo propInfo, string fieldName, IEnumerable<string> aliases = null)
        {
            this.Info = propInfo;
            this.FieldName = String.IsNullOrEmpty(fieldName) ? propInfo.Name : fieldName;
            this.Aliases = new List<string>()
            {
                propInfo.Name
            };

            if (aliases != null)
            {
                foreach (string alias in aliases)
                {
                    this.Aliases.Add(alias);
                }
            }
        }
    }
}
